Dino Geek, try to help you

How to redirect a page with `.htaccess`?


To redirect a page using `.htaccess`, you must create or edit the `.htaccess` file in the root directory of your website. This file uses Apache’s mod\_rewrite module to handle URL redirections. Here are several ways to achieve different types of redirects:

  1. 301 Redirect (Permanent)
    A 301 redirect signals to search engines that the page has been permanently moved to a new location. This is the most commonly used type of redirect for SEO purposes. To add a 301 redirect in your `.htaccess` file, use the following syntax:

```
Redirect 301 /old-page.html http://www.yourwebsite.com/new-page.html
```

Example:
```
Redirect 301 /about.html http://www.yourwebsite.com/about-us.html
```

  1. 302 Redirect (Temporary)
    A 302 redirect is used when a page is temporarily moved. It doesn’t pass link equity like a 301 redirect, but it’s useful if the page is going to be back at its original URL at some point.

```
Redirect 302 /temporarily-moved-page.html http://www.yourwebsite.com/new-temporary-page.html
```

Example:
```
Redirect 302 /sale.html http://www.yourwebsite.com/summer-sale.html
```

  1. Redirect with mod\_rewrite
    For more complex redirects, you can use mod_rewrite to create rules using regular expressions. Ensure that the mod_rewrite module is enabled. Add the following lines at the top of your `.htaccess` file to enable rewrite:

```
RewriteEngine On
```

  1. Redirect a Single Page
    ```
    RewriteRule ^old-page.html$ http://www.yourwebsite.com/new-page.html [R=301,L]
    ```

Example:
```
RewriteRule ^contact.html$ http://www.yourwebsite.com/get-in-touch.html [R=301,L]
```

  1. Redirect an Entire Directory
    If you want to redirect an entire directory to a new location, use the following rule:

```
RewriteRule ^old-directory/(.*)$ http://www.yourwebsite.com/new-directory/$1 [R=301,L]
```

Example:
```
RewriteRule ^blog/(.*)$ http://www.yourwebsite.com/news/$1 [R=301,L]
```

  1. Redirect All Pages to a New Domain
    This rule will redirect all traffic from the old domain to a new domain:

```
RewriteCond %{HTTP_HOST} ^old-domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.old-domain.com$
RewriteRule (.*)$ http://www.new-domain.com/$1 [R=301,L]
```

Example:
```
RewriteCond %{HTTP_HOST} ^example-old.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example-old.com$
RewriteRule (.*)$ http://www.example-new.com/$1 [R=301,L]
```

  1. Redirect Non-www to www (or vice versa)
    To standardize URLs and avoid duplicate content issues, you might want to redirect all your non-www traffic to www (or vice versa).

  1. Non-www to www:
    ```
    RewriteCond %{HTTP_HOST} ^yourwebsite.com [NC]
    RewriteRule ^(.*)$ http://www.yourwebsite.com/$1 [L,R=301]
    ```

  1. www to Non-www:
    ```
    RewriteCond %{HTTP_HOST} ^www.yourwebsite.com [NC]
    RewriteRule ^(.*)$ http://yourwebsite.com/$1 [L,R=301]
    ```

  1. Useful Sources:
    1. Apache HTTP Server Documentation: Comprehensive guide for `.htaccess` and mod\_rewrite. [Apache HTTP Server Version 2.4 Documentation](https://httpd.apache.org/docs/2.4/howto/htaccess.html)

1. Moz Blog: Provides best practices for using redirects, particularly focusing on their SEO implications. [Moz – Permanent (301) VS Temporary (302) Redirects](https://moz.com/learn/seo/redirection)

1. Stack Overflow: Community-driven, detailed discussions on various `.htaccess` use cases. [Stack Overflow – .htaccess redirect](https://stackoverflow.com/questions/tagged/htaccess)

By utilizing the configurations and examples provided, you can effectively manage URL redirections on your Apache server.


Simply generate articles to optimize your SEO
Simply generate articles to optimize your SEO





DinoGeek offers simple articles on complex technologies

Would you like to be quoted in this article? It's very simple, contact us at dino@eiki.fr

CSS | NodeJS | DNS | DMARC | MAPI | NNTP | htaccess | PHP | HTTPS | Drupal | WEB3 | LLM | Wordpress | TLD | Domain name | IMAP | TCP | NFT | MariaDB | FTP | Zigbee | NMAP | SNMP | SEO | E-Mail | LXC | HTTP | MangoDB | SFTP | RAG | SSH | HTML | ChatGPT API | OSPF | JavaScript | Docker | OpenVZ | ChatGPT | VPS | ZIMBRA | SPF | UDP | Joomla | IPV6 | BGP | Django | Reactjs | DKIM | VMWare | RSYNC | Python | TFTP | Webdav | FAAS | Apache | IPV4 | LDAP | POP3 | SMTP

| Whispers of love (API) | Déclaration d'Amour |






Legal Notice / General Conditions of Use